﻿using System;
//①、创建一个自定义特性：
// 描述如何使用一个自定义特性 SomethingAttribute
[AttributeUsage(AttributeTargets.All,AllowMultiple = true, Inherited = true)]

// ******** 自定义特性SomethingAttribute ********
public class SomethingAttribute : Attribute
{
    private string name; // 名字
    private string data; // 日期
    public string Name
    {
        get { return name; }
        set { name = value; }
    }
    public string Data
    {
        get { return data; }
        set { data = value; }
    }
    public SomethingAttribute(string name)
    {
        this.name = name;
    }
}

//②、实例化自定义特性
[Something("Amy", Data = "2018-06-18")]
[Something("Jack", Data = "2018-06-18")]
class Test { }

//③、获取自定义特性中的变量
class Program
{
    public static void Main()
    {
        Type t = typeof(Test);
        var something = t.GetCustomAttributes(typeof(SomethingAttribute), true);
        foreach(SomethingAttribute each in something)
        {
            Console.WriteLine("Name: {0}", each.Name);
            Console.WriteLine("Data: {0}", each.Data);
        }
        Console.ReadKey();
    }
}